package com.project.junM.service.impl;

import com.project.junM.entity.*;
import com.project.junM.mapper.*;
import com.project.junM.service.TMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TMemberServiceImpl implements TMemberService {

    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TMemberInfoMapper tMemberInfoMapper;
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TMemberExaminationMapper tMemberExaminationMapper;
    @Autowired
    private TMemberDynamicinfoMapper tMemberDynamicinfoMapper;
    @Autowired
    private TSetmealMapper tSetmealMapper;
    @Autowired
    private THealthmgrMapper tHealthmgrMapper;

    @Override
    public boolean queryByPhoneNumber(String phoneNumber) {

        Integer i = tMemberMapper.queryByPhoneNumber(phoneNumber);

        boolean flag=i>0?true:false;

        return flag;
    }

    @Override
    public boolean queryByIdCard(String idCard) {
        Integer i = tMemberMapper.queryByIdCard(idCard);

        boolean flag=i>0?true:false;

        return flag;
    }

    @Override
    public TMember queryByPhoneNumberPassWord(String phoneNumber, String passWord, HttpSession session) {

        TMember tMember = tMemberMapper.queryByPhoneNumberPassWord(phoneNumber, passWord);
        boolean flag=false;
        if(tMember!=null){
//            System.out.println(tMember);
            session.setAttribute("user",tMember);
            flag = true;
        }
        return tMember;
    }

    @Override
    public boolean insertSelective(TMember record) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        record.setRegtime(format);
        //注册，并且放回主键
        int i = tMemberMapper.insertSelective(record);
        Integer id = record.getId();

        TMemberInfo tMemberInfo = new TMemberInfo();
        tMemberInfo.setTMemberId(id);
        tMemberInfo.setStatus(0);
        tMemberInfoMapper.insertSelective(tMemberInfo);

        TMemberExamination tMemberExamination = new TMemberExamination();
        tMemberExamination.setTMemberId(id);
        tMemberExamination.setReportStatus(0);
        tMemberExaminationMapper.insertSelective(tMemberExamination);

        //会员编号
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd");
        String vidId = simpleDateFormat1.format(new Date());
        String vipStr=id.toString();
        String vipStr0="";//不足3位的补0
        if(vipStr.length()<3){
            for(int k=0;k<3-vipStr.length();k++){
                vipStr0+=0;
            }
        }
        String filenumber = vidId + vipStr0 + vipStr;
        record.setFilenumber(filenumber);
        tMemberMapper.updateByPrimaryKeySelective(record);

        boolean flag=i>0?true:false;
        return flag;
    }

    @Override
    public boolean updateByPrimaryKeySelective(TMember record, HttpSession session) {
        TMember user = (TMember)session.getAttribute("user");
        boolean flag=user!=null?true:false;
        if(flag){
            Integer id = user.getId();
            record.setId(id);
            Integer maritalStatus = record.getTMemberInfo().getMaritalStatus();
            Date orderdate = record.getTOrder().getOrderdate();

            tMemberMapper.updateByPrimaryKeySelective(record);
            Integer setmealId = record.getSetmealId();

            //婚姻状况
            TMemberInfo tMemberInfo = tMemberInfoMapper.selectByMemberId(id);
            tMemberInfo.setMaritalStatus(maritalStatus);
            tMemberInfoMapper.updateByPrimaryKeySelective(tMemberInfo);
            //预约时间、预约方式
            TOrder tOrder = new TOrder();
            tOrder.setMemberId(id);
            tOrder.setOrderdate(orderdate);
            tOrder.setOrdertype("微信预约");
            tOrder.setOrderstatus("未到诊");
            tOrder.setSetmealId(setmealId);
            tOrderMapper.insertSelective(tOrder);
            //确认套装
            TMemberExamination tMemberExamination = tMemberExaminationMapper.selectByMemberId(id);
            tMemberExamination.setTSetmealId(setmealId);
            tMemberExamination.setExamDate(orderdate);
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String format = dateFormat.format(date);
            try {
                tMemberExamination.setCreatetime(dateFormat.parse(format));
            } catch (Exception e) {
                e.printStackTrace();
            }
            tMemberExaminationMapper.updateByPrimaryKeySelective(tMemberExamination);
        }
        return flag;
    }

    @Override
    public TMember queryMemberAlls(HttpSession session) {

        TMember user = (TMember) session.getAttribute("user");
        Integer id = user.getId();
        TMember tMember = this.tMemberMapper.selectByPrimaryKey(id);
        return tMember;
    }

    @Override
    public TMember selectByPrimaryKey(HttpSession session) {
        TMember user = (TMember) session.getAttribute("user");
        //查询是否结婚
        TMemberInfo tMemberInfo = tMemberInfoMapper.selectByMemberId(user.getId());
        TMember tMember = tMemberMapper.selectByPrimaryKey(user.getId());
        tMember.setTMemberInfo(tMemberInfo);
        //查看身高体重
        TMemberDynamicinfo tMemberDynamicinfo = tMemberDynamicinfoMapper.selectByMemberId(user.getId());
        System.out.println(tMemberDynamicinfo);
        tMember.setTMemberDynamicinfo(tMemberDynamicinfo);
        return tMember;
    }

    @Override
    public TMember queryById(Integer id) {
        return tMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public TMember queryReprotByMemberId(HttpSession session) {
        TMember tMember = (TMember) session.getAttribute("user");
        TMember user = tMemberMapper.selectByPrimaryKey(tMember.getId());
        Integer memberId = user.getId();
        TMemberInfo tMemberInfo = tMemberInfoMapper.selectByMemberId(memberId);
        Integer healthMgrId  = tMemberInfo.getHealthMgrId();
        THealthmgr tHealthmgr = tHealthmgrMapper.selectByHealthMgrId(healthMgrId);
        String healthMgrName = tHealthmgr.getHealthMgrName();
        user.setTMemberInfo(tMemberInfo);
        user.setHealthName(healthMgrName);
        TMemberExamination tMemberExamination = tMemberExaminationMapper.selectByMemberId(memberId);
        Integer tSetmealId = tMemberExamination.getTSetmealId();
        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(tSetmealId);
        user.setTSetmeal(tSetmeal);
        TOrder tOrder = tOrderMapper.selectByMemberId(memberId);
        user.setTOrder(tOrder);
        return user;
    }
}
